package com.hyt.it.ogt.pj.data.asynchronous;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.hyt.core.constant.CommonConstant;
import com.hyt.it.ogt.pj.data.utils.ExecutorPoolUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @program: it-ogt-pj-data
 * @description: 这个类用来做CommandLineRunner、DisposableBean的业务，如需用到上面的操作请继承这个类
 * @create: 2022-06-17 16:54
 **/
@Slf4j
@Component
public class DestroyBean implements CommandLineRunner, DisposableBean {

    @Override
    public void run(String... args) throws Exception {

    }

    @Override
    public void destroy() throws Exception {
    	ExecutorService executorService = ExecutorPoolUtil.getExecutorService();
        executorService.shutdown();
        // 这里进行等候一定时间给正在执行的任务执行完成
        if (!executorService.awaitTermination(CommonConstant.TWO, TimeUnit.SECONDS)) {
            executorService.shutdownNow();
        }
        log.info("# global executor service destroy ...");
    }
}
